package com.crane.oss.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crane.oss.sys.bean.resp.UserRoleBean;
import com.crane.oss.sys.dao.MenuApiDao;
import com.crane.oss.sys.dao.RoleDao;
import com.crane.oss.sys.dao.UserRoleDao;
import com.crane.oss.sys.model.MenuApi;
import com.crane.oss.sys.model.UserRole;
import com.crane.oss.sys.service.UserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bealon
 */
@Service("userRoleService")
public class UserRoleServiceImpl  extends ServiceImpl<UserRoleDao, UserRole> implements UserRoleService {

    private static final Logger logger = LoggerFactory.getLogger(UserRoleServiceImpl.class);

    @Resource
    protected UserRoleDao userRoleDao;

    @Resource
    protected RoleDao roleDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long userId, List<Long> roleIdList, Long createUserId) {
        logger.info("UserRoleServiceImpl validatorRole roleList={}", roleIdList);
        if (roleIdList.size() == 0) {
            return;
        }
        // 先删除用户与角色关系
        userRoleDao.delete(Wrappers.lambdaQuery(UserRole.class).eq(UserRole ::getUserId,userId));
        // 保存用户与角色关系
        Set<Long> roleIdSet = new HashSet<>(roleIdList);


        // 保存角色与菜单关系
        List<UserRole> userRoleList = new ArrayList<>();
        for (Long roleId : roleIdSet) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRole.setCreateUserId(createUserId);
            userRole.setCreateTime(new Date());
            userRoleList.add(userRole);
        }
        this.saveBatch(userRoleList);
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        logger.info("UserRoleServiceImpl queryRoleIdList userId={}", userId);
        List<UserRole> userRoleList = userRoleDao.selectList(Wrappers.lambdaQuery(UserRole.class).eq(UserRole ::getUserId,userId));
        List<Long> idList = new ArrayList<>();
        for (UserRole userRole : userRoleList) {
            idList.add(userRole.getRoleId());
        }
        return idList;
    }

    @Override
    public List<Long> queryUserIdList(Long roleId) {
        logger.info("UserRoleServiceImpl queryUserIdList roleId={}", roleId);
        List<UserRole> userRoleList = userRoleDao.selectList(Wrappers.lambdaQuery(UserRole.class).eq(UserRole ::getRoleId,roleId));
        List<Long> idList = new ArrayList<>();
        for (UserRole userRole : userRoleList) {
            idList.add(userRole.getUserId());
        }
        return idList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByUserId(Long userId) {
        logger.info("UserRoleServiceImpl deleteByUserId userId={}", userId);
        userRoleDao.delete(Wrappers.lambdaQuery(UserRole.class).eq(UserRole ::getUserId,userId));
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        logger.info("UserRoleServiceImpl deleteByRoleId roleId={}", roleId);
        userRoleDao.delete(Wrappers.lambdaQuery(UserRole.class).eq(UserRole ::getRoleId,roleId));
    }

    @Override
    public List<UserRoleBean> queryRoleIdList(List<Long> userIds) {
        return roleDao.getUsersRole(userIds);
    }

}
